/*
 * Copyright (C) 2014 Lucid Wolf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.lucidhex.equation.parser;

import java.util.ArrayList;

/**
 *  
 * @author Lucid Wolf
 */
public class ExpScanner {
// Scan lexical tokens in input strings.


    private String s;
    private String operatorChars;

    protected ArrayList tokens = new ArrayList();
    protected int index = -1;

    public ExpScanner(String string, String operatorChars) {
        this.s = string;
	this.operatorChars = operatorChars;

        int i = 0;
	do {
	    i = scanToken(i);
	} while (i < s.length());
    }

    public String getInput() {
	return s;
    }

    // The tokens may have been diddled, so this can be different from 
    // getInput().
    @Override
    public String toString() {
	StringBuilder sb = new StringBuilder();
	int whitespace = 0;
	for (int i = 0; i < tokens.size(); ++i) {
	    Token t = (Token) tokens.get(i);

	    int spaces = (whitespace != 0 ? whitespace : t.leadingWhitespace);
	    if (i == 0) {
                spaces = 0;
            }
	    else if (spaces == 0 && !joinable((Token) tokens.get(i-1), t)) {
                spaces = 1;
            }
	    for (int j = spaces; 0 < j; --j) {
                sb.append(" ");
            }

	    sb.append(t.sval);
	    whitespace = t.trailingWhitespace;
	}
	return sb.toString();
    }

    private boolean joinable(Token s, Token t) {
	return !(isAlphanumeric(s) && isAlphanumeric(t));
    }

    private boolean isAlphanumeric(Token t) {
	return t.ttype == Token.TT_WORD || t.ttype == Token.TT_NUMBER;
    }

    public boolean isEmpty() {
	return tokens.isEmpty();
    }

    public boolean atStart() {
	return index <= 0;
    }

    public boolean atEnd() {
	return tokens.size() <= index;
    }

    public Token nextToken() {
	++index;
	return getCurrentToken();
    }

    public Token getCurrentToken() {
	if (atEnd()) {
            return new Token(Token.TT_EOF, 0, s, s.length(), s.length());
        }
	return (Token) tokens.get(index);
    }

    private int scanToken(int i) {
        while (i < s.length() && Character.isWhitespace(s.charAt(i))) {
            ++i;
        }

        if (i == s.length()) {
	    return i;
        } else if (0 <= operatorChars.indexOf(s.charAt(i))) {
	    if (i+1 < s.length()) {
		String pair = s.substring(i, i+2);
		int ttype = 0;
                switch (pair) {
                    case "<=":
                        ttype = Token.TT_LE;
                        break;
                    case ">=":
                        ttype = Token.TT_GE;
                        break;
                    case "<>":
                        ttype = Token.TT_NE;
                        break;
                }
		if (0 != ttype) {
		    tokens.add(new Token(ttype, 0, s, i, i+2));
		    return i+2;
		}
	    }
	    tokens.add(new Token(s.charAt(i), 0, s, i, i+1));
            return i+1;
        } else if (Character.isLetter(s.charAt(i))) {
            return scanSymbol(i);
        } else if (Character.isDigit(s.charAt(i)) || '.' == s.charAt(i)) {
            return scanNumber(i);
        } else {
            tokens.add(makeErrorToken(i, i+1));
            return i+1;
        }
    }

    private int scanSymbol(int i) {
	int from = i;
        while (i < s.length() 
	       && (Character.isLetter(s.charAt(i))
		   || Character.isDigit(s.charAt(i)))) {
            ++i;
        }
	tokens.add(new Token(Token.TT_WORD, 0, s, from, i));
	return i;
    }

    private int scanNumber(int i) {
	int from = i;

        // We include letters in our purview because otherwise we'd
        // accept a word following with no intervening space.
        for (; i < s.length(); ++i) {
            if ('.' != s.charAt(i)
                && !Character.isDigit(s.charAt(i))
                && !Character.isLetter(s.charAt(i))) {
                break;
            }
        }

        String text = s.substring(from, i);
	double nval;
        try {
            nval = Double.valueOf(text).doubleValue();
        } catch (NumberFormatException nfe) {
            tokens.add(makeErrorToken(from, i));
	    return i;
        }

	tokens.add(new Token(Token.TT_NUMBER, nval, s, from, i));
	return i;
    }

    private Token makeErrorToken(int from, int i) {
	return new Token(Token.TT_ERROR, 0, s, from, i);
    }
}
